package com.flxz.user.controller;

import com.flxz.common.annotation.Log;
import com.flxz.common.core.controller.BaseController;
import com.flxz.common.core.domain.AjaxResult;
import com.flxz.common.core.page.TableDataInfo;
import com.flxz.common.enums.BusinessType;
import com.flxz.user.service.IUserTipsService;
import com.flxz.user.domain.vo.TipsListVO;
import com.flxz.user.domain.vo.TipsDetailVO;
import com.flxz.user.domain.vo.TipsPublishVO;
import com.flxz.user.domain.vo.ReplyVO;
import com.flxz.user.domain.vo.LikeVO;
import com.flxz.user.domain.vo.FavoriteVO;
import com.flxz.user.domain.vo.ReportVO;
import com.flxz.user.domain.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户端省薪贴Controller
 * 
 * @author flxz
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/user/forum/tips")
public class UserTipsController extends BaseController
{
    @Autowired
    private IUserTipsService userTipsService;

    /**
     * 获取省薪贴列表
     */
    @GetMapping("/list")
    public TableDataInfo getTipsList(TipsListVO query)
    {
        startPage();
        List<TipsListVO> list = userTipsService.getTipsList(query);
        return getDataTable(list);
    }

    /**
     * 获取省薪贴详情
     */
    @GetMapping("/{topicId}")
    public AjaxResult getTipsDetail(@PathVariable Long topicId)
    {
        TipsDetailVO detail = userTipsService.getTipsDetail(topicId);
        return success(detail);
    }

    /**
     * 发布省薪贴
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:add')")
    @Log(title = "发布省薪贴", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult publishTips(@RequestBody TipsPublishVO tipsVO)
    {
        Long userId = getUserId();
        tipsVO.setUserId(userId);
        return toAjax(userTipsService.publishTips(tipsVO));
    }

    /**
     * 获取分类列表
     */
    @GetMapping("/category/list")
    public AjaxResult getCategoryList()
    {
        List<CategoryVO> list = userTipsService.getCategoryList();
        return success(list);
    }

    /**
     * 获取回复列表
     */
    @GetMapping("/reply/list/{topicId}")
    public TableDataInfo getReplyList(@PathVariable Long topicId, ReplyVO query)
    {
        startPage();
        query.setTopicId(topicId);
        List<ReplyVO> list = userTipsService.getReplyList(query);
        return getDataTable(list);
    }

    /**
     * 发布回复
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:reply')")
    @Log(title = "发布回复", businessType = BusinessType.INSERT)
    @PostMapping("/reply")
    public AjaxResult publishReply(@RequestBody ReplyVO replyVO)
    {
        Long userId = getUserId();
        replyVO.setUserId(userId);
        return toAjax(userTipsService.publishReply(replyVO));
    }

    /**
     * 点赞/取消点赞
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:like')")
    @Log(title = "点赞操作", businessType = BusinessType.UPDATE)
    @PostMapping("/like")
    public AjaxResult toggleLike(@RequestBody LikeVO likeVO)
    {
        Long userId = getUserId();
        likeVO.setUserId(userId);
        return toAjax(userTipsService.toggleLike(likeVO));
    }

    /**
     * 收藏/取消收藏
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:favorite')")
    @Log(title = "收藏操作", businessType = BusinessType.UPDATE)
    @PostMapping("/favorite")
    public AjaxResult toggleFavorite(@RequestBody FavoriteVO favoriteVO)
    {
        Long userId = getUserId();
        favoriteVO.setUserId(userId);
        return toAjax(userTipsService.toggleFavorite(favoriteVO));
    }

    /**
     * 举报内容
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:report')")
    @Log(title = "举报内容", businessType = BusinessType.INSERT)
    @PostMapping("/report")
    public AjaxResult reportContent(@RequestBody ReportVO reportVO)
    {
        Long userId = getUserId();
        reportVO.setUserId(userId);
        return toAjax(userTipsService.reportContent(reportVO));
    }

    /**
     * 增加浏览次数
     */
    @PostMapping("/view/{topicId}")
    public AjaxResult increaseViewCount(@PathVariable Long topicId)
    {
        return toAjax(userTipsService.increaseViewCount(topicId));
    }

    /**
     * 获取热门省薪贴
     */
    @GetMapping("/hot")
    public AjaxResult getHotTips()
    {
        List<TipsListVO> list = userTipsService.getHotTips();
        return success(list);
    }

    /**
     * 获取我的省薪贴
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:my')")
    @GetMapping("/my")
    public TableDataInfo getMyTips(TipsListVO query)
    {
        startPage();
        Long userId = getUserId();
        query.setUserId(userId);
        List<TipsListVO> list = userTipsService.getMyTips(query);
        return getDataTable(list);
    }

    /**
     * 获取我的收藏
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:my')")
    @GetMapping("/favorite/my")
    public TableDataInfo getMyFavorites(TipsListVO query)
    {
        startPage();
        Long userId = getUserId();
        query.setUserId(userId);
        List<TipsListVO> list = userTipsService.getMyFavorites(query);
        return getDataTable(list);
    }

    /**
     * 更新我的省薪贴
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:edit')")
    @Log(title = "更新省薪贴", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateMyTips(@RequestBody TipsPublishVO tipsVO)
    {
        Long userId = getUserId();
        tipsVO.setUserId(userId);
        return toAjax(userTipsService.updateMyTips(tipsVO));
    }

    /**
     * 删除我的省薪贴
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:forum:remove')")
    @Log(title = "删除省薪贴", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicIds}")
    public AjaxResult deleteMyTips(@PathVariable Long[] topicIds)
    {
        Long userId = getUserId();
        return toAjax(userTipsService.deleteMyTips(topicIds, userId));
    }
}
